﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.CBO.SCM.Supplier;
using UFIDA.U9.Cust.YYD.PlugBE.Dtos;
using UFIDA.U9.Cust.YYD.PlugBE.Public;
using UFSoft.UBF.Business;

namespace UFIDA.U9.Cust.YYD.PlugBE.SupplierP
{
    public class AfterInserting : UFSoft.UBF.Eventing.IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            var holder = key.GetEntity() as UFIDA.U9.CBO.SCM.Supplier.Supplier;
            if (holder == null)
                return;
            #endregion
            HttpHelper httpHelper = new HttpHelper();
            JsonConfig config = new JsonConfig();
            httpHelper.URL = config.SelectToken("WMS.URL")+ "/openapi/sync/supplier/save";
            WMSSupplierDTO dto = new WMSSupplierDTO();
            dto.code = holder.Code;
            dto.text = holder.Name;
            dto.abbr = holder.ShortName;
            //dto.remark = holder.;

            var SupplierItemList = SupplierItem.Finder.FindAll($"SupplierInfo.Supplier = {holder.ID}");

            foreach (var item in SupplierItemList)
            {
                dto.materialCodes.Add(item.ItemInfo.ItemCode);
            }
            foreach (var site in holder.SupplierSites)
            {
                Addresslist al = new Addresslist();
                al.code = site.Code;
                al.name = site.Name;
                dto.addressList.Add(al);
            }
            httpHelper.Content = JsonConvert.SerializeObject(dto);
            var result = httpHelper.Request();
        }
    }
}
